// 顺序栈头文件

#include <stdio.h>
#include <malloc.h>

#define STACK_INIT_SIZE 50
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0

typedef int Status;
typedef int SElemType;

typedef struct {
    SElemType *base;//基址
    SElemType *top;//栈顶指针
    int stacksize;
} SqStack;

Status InitStack(SqStack &S) {
    S.base = (SElemType *) malloc(STACK_INIT_SIZE * sizeof(SElemType));
    if (S.base == NULL)
        return ERROR;
    else {
        S.top = S.base;
        S.stacksize = STACK_INIT_SIZE;
    }
    return OK;
} //初始化一个空栈

Status GetTop(SqStack S, SElemType &e) {
    if (S.base == S.top)
        return ERROR;
    e = *(S.top - 1);
    return OK;
}  //获取栈顶元素

Status Push(SqStack &S, SElemType e) {
    SElemType *temp;
    if (S.top - S.base >= S.stacksize) {
        temp = (SElemType *) realloc(S.base, (S.stacksize + STACKINCREMENT) * sizeof(SElemType));
        if (temp == NULL)
            return ERROR;
        S.base = temp;
        S.top = S.base + S.stacksize;
        S.stacksize += STACKINCREMENT;
    }
    *S.top++ = e;
    return OK;
}  //将元素e入栈

Status Pop(SqStack &S, SElemType &e) {
    if (S.base == S.top)
        return ERROR;
    else {
        e = *--S.top;
        return OK;
    }
}  //将栈顶元素出栈，并将其值返回在e中

int StackLength(SqStack S) {
    return S.top - S.base;
}  //求栈的长度